package com.wnnight.wys.web.repairs;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.wnnight.wys.entity.repairs.RepairsType;
import com.wnnight.wys.groupInterface.AddGroup;
import com.wnnight.wys.groupInterface.AlterGroup;
import com.wnnight.wys.service.repairs.RepairsTypeService;
import com.wnnight.wys.util.repairs.RepairsUtil;
import com.wnnight.wys.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * @author rzh6230
 * 2022/8/27 20:21
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/bx/type")
@SuppressWarnings("all")
public class RepairsTypeController {
    @Autowired
    private RepairsTypeService repairsTypeService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @PostMapping("/list")
    @PreAuthorize("hasAuthority('bx:type:list')")
    public ResultVo<?> list() throws JsonProcessingException {
        String s = stringRedisTemplate.opsForValue().get("repairsType");
        if ( s == null){
            log.debug("redis中没有");
            List<RepairsType>  list = repairsTypeService.list();
            s =  RepairsUtil.objectMapper.writeValueAsString(list);
            stringRedisTemplate.opsForValue().set("repairsType",s);
            log.debug("查询后，存入redis");
        }
        return new ResultVo<>(s);
    }

    @PostMapping("/add")
    @PreAuthorize("hasAuthority('bx:type:add')")
    public ResultVo<?> add(@RequestBody @Validated(AddGroup.class) RepairsType repairsType) throws JsonProcessingException {
        repairsTypeService.save(repairsType);
        List<RepairsType> list = repairsTypeService.list();
        String s =  RepairsUtil.objectMapper.writeValueAsString(list);
        stringRedisTemplate.opsForValue().set("repairsType",s);
        return ResultVo.SUCCESS;
    }

    @PostMapping("/update")
    @PreAuthorize("hasAuthority('bx:type:update')")
    public ResultVo<?> update(@RequestBody @Validated(AlterGroup.class) RepairsType repairsType) throws JsonProcessingException {
        repairsTypeService.updateById(repairsType);
        List<RepairsType> list = repairsTypeService.list();
        String s =  RepairsUtil.objectMapper.writeValueAsString(list);
        stringRedisTemplate.opsForValue().set("repairsType",s);
        return ResultVo.SUCCESS;
    }

    @PostMapping("/delete")
    @PreAuthorize("hasAuthority('bx:type:delete')")
    public ResultVo<?> delete(@RequestBody Integer[] ids) throws JsonProcessingException {
        repairsTypeService.removeBatchByIds(Arrays.asList(ids));
        List<RepairsType> list = repairsTypeService.list();
        String s =  RepairsUtil.objectMapper.writeValueAsString(list);
        stringRedisTemplate.opsForValue().set("repairsType",s);
        return ResultVo.SUCCESS;
    }
}
